/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/render', 'N/file'], function (record, render, file) {
    function beforeLoad(context) {
        if (context.type === 'view') {
            try {
                var rec = record.load({
                    type: record.Type.INVENTORY_ADJUSTMENT,
                    id: context.newRecord.id,
                    isDynamic: true
                });
                var invAdjust = {};
                invAdjust.printTimes = rec.getValue('custbody_print_times');

                invAdjust.memo = rec.getText('memo');

                invAdjust.department = rec.getText('department');
                invAdjust.subsidiary = rec.getText('subsidiary');

                var subsidiaryRec = record.load({
                    type: 'subsidiary',
                    id: rec.getValue('subsidiary')
                });
                invAdjust.currency = subsidiaryRec.getText('currency');

                invAdjust.date = rec.getText('trandate');
                invAdjust.tranid = rec.getValue('tranid');

                var adjustDetail = [];
                var totalQty = 0;
                var totalAmount = 0;
                var lineCount = rec.getLineCount('inventory');
                for (var i = 0; i < lineCount; i++) {
                    var adjustLine = {};
                    rec.selectLine('inventory', i);
                    adjustLine.lineNumber = Number(i + 1);
                    adjustLine.item = rec.getCurrentSublistText('inventory', 'item');
                    adjustLine.location = rec.getCurrentSublistText('inventory', 'location');
                    adjustLine.quantity = Number(rec.getCurrentSublistText('inventory', 'adjustqtyby').replace(/,/g, ""));
                    totalQty += adjustLine.quantity;
                    adjustLine.unit = rec.getCurrentSublistText('inventory', 'units');
                    adjustLine.unitcost = Number(rec.getCurrentSublistText('inventory', 'unitcost').replace(/,/g, ""));
                    adjustLine.amount = Number((adjustLine.quantity * adjustLine.unitcost).toFixed(2));
                    totalAmount += adjustLine.amount;
                    adjustLine.memo = rec.getCurrentSublistText('inventory', 'custcol_remarks');
                    try {
                        var item = record.load({
                            type: 'assemblyitem',
                            id: rec.getCurrentSublistValue('inventory', 'item'),
                            isDynamic: true
                        });
                        adjustLine.description = item.getText('displayname');
                        adjustLine.color = item.getText('custitem_color');
                        adjustLine.colorNumber = item.getText('custitem_color_number');
                        adjustLine.size = item.getText('custitem_size');
                    } catch (e) {
                        try {
                            var item = record.load({
                                type: 'inventoryitem',
                                id: rec.getCurrentSublistValue('inventory', 'item'),
                                isDynamic: true
                            });
                            adjustLine.description = item.getText('displayname');
                            adjustLine.color = item.getText('custitem_color');
                            adjustLine.colorNumber = item.getText('custitem_color_number');
                            adjustLine.size = item.getText('custitem_door_width');
                        } catch (e) {

                        }

                    }
                    adjustDetail.push(adjustLine);
                }
                invAdjust.totalQty = totalQty;
                invAdjust.totalAmount = totalAmount;
                invAdjust.adjustDetail = adjustDetail;
                log.debug('invAdjust', invAdjust);

                //调用模板文件
                var renderer = render.create();
                renderer.templateContent = file.load(1157).getContents();
                renderer.addCustomDataSource({
                    format: render.DataSource.JSON,
                    alias: 'invAdjust',
                    data: JSON.stringify(invAdjust)
                });
                var pdfFile = renderer.renderAsPdf();
                //PDF文件存放文件夹ID
                pdfFile.folder = 185;
                pdfFile.name = context.newRecord.id + new Date().getTime() + '.pdf';
                var fileId = pdfFile.save();
                var invalidFile = record.create({type: 'customrecord_invalid_file_id'});
                invalidFile.setValue('custrecord_invalid_file_id', fileId);
                invalidFile.save();
                var File = file.load(fileId);
                var pdfurl = File.url;
                var form = context.form;
                form.addButton({
                    id: 'custpage_button',
                    label: '打印库存调整单',
                    functionName: "window.open('" + pdfurl + "');"
                });
            } catch (e) {
                log.error(e.name, e.message);
            }

        }
    }


    return {
        beforeLoad: beforeLoad
    };
});
